﻿@page "/Grid/Summary/Group"
@layout DataProviderAccessArea<INwindDataProvider>

<DemoPageSectionComponent Id="Grid-Summary-Group" ShowSizeMode="true">
    <ChildContentWithParameters Context="Params">
        @inject NwindDataService NwindDataService

        <div class="grid-container">
            <DxGrid @ref="Grid"
                    Data="@Data"
                    PageSize="20"
                    ShowGroupPanel="true"
                    SizeMode="Params.SizeMode"
                    ColumnResizeMode="GridColumnResizeMode.NextColumn"
                    TextWrapEnabled="false">
                <Columns>
                    <DxGridDataColumn FieldName="Country" GroupIndex="0" Width="10%" />
                    <DxGridDataColumn FieldName="City" GroupIndex="1" Width="10%" />
                    <DxGridDataColumn FieldName="CompanyName" MinWidth="100" />
                    <DxGridDataColumn FieldName="UnitPrice" DisplayFormat="c" Width="10%" />
                    <DxGridDataColumn FieldName="Quantity" Width="10%" />
                    <DxGridDataColumn FieldName="Total"
                                      UnboundType="GridUnboundColumnType.Decimal"
                                      UnboundExpression="[UnitPrice] * [Quantity]"
                                      DisplayFormat="c"
                                      MinWidth="100"
                                      Width="15%" />
                </Columns>
                <GroupSummary>
                    <DxGridSummaryItem SummaryType="GridSummaryItemType.Count" FieldName="CompanyName" />
                    <DxGridSummaryItem SummaryType="GridSummaryItemType.Sum" FieldName="Total" />
                </GroupSummary>
            </DxGrid>
        </div>

        @code {
            object Data { get; set; }
            IGrid Grid { get; set; }

            protected override async Task OnInitializedAsync() {
                var invoices = await NwindDataService.GetInvoicesAsync();
                var customers = await NwindDataService.GetCustomersAsync();
                Data = invoices.OrderBy(i => i.OrderDate).Join(customers, i => i.CustomerId, c => c.CustomerId, (i, c) => {
                    return new {
                        CompanyName = c.CompanyName,
                        City = i.City,
                        Region = i.Region,
                        Country = i.Country,
                        UnitPrice = i.UnitPrice,
                        Quantity = i.Quantity
                    };
                });
            }
        }
    </ChildContentWithParameters>
</DemoPageSectionComponent>
